//index.js
//获取应用实例
const api = require('../../utils/api');
const app = getApp();
const constant = require('../../utils/constant');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // search高度
    searchHeight: 0,
    // flash
    flash: [
      {id:1,src: '../../assets/img/flash2.jpg'},
      {id:2,src: '../../assets/img/flash.jpg'}
    ],
    // menu
    menu:[
      // {id: 1,icon: 'iconfont iconicon--',title: '特价茅台',link: '',img: '../../assets/img/m1.png'},
      // {id: 1,icon: 'iconfont iconicon--',title: '老茅台',link: '',img: '../../assets/img/m2.png',screen:{goods_category_id:null,goods_category_extend_id:[],brand_id:null}},
      // {id: 2,icon: 'iconfont iconicon--',title: '老五粮液',link: '',img: '../../assets/img/m3.png',screen:{goods_category_id:null,goods_category_extend_id:[],brand_id:null}},
      // {id: 3,icon: 'iconfont iconicon--',title: '陈年老酒',link: '',img: '../../assets/img/m10.png',screen:{goods_category_id:null,goods_category_extend_id:[12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27],brand_id:null}},
      {id: 4,icon: 'iconfont iconicon--',title: '白酒',link: '',img: '../../assets/img/m7.png',screen:{goods_category_id:[2],goods_category_extend_id:[],brand_id:null}},
      {id: 5,icon: 'iconfont iconicon--',title: '红酒',link: '',img: '../../assets/img/m5.png',screen:{goods_category_id:[5],goods_category_extend_id:[],brand_id:null}},
      {id: 6,icon: 'iconfont iconicon--',title: '威士忌',link: '',img: '../../assets/img/m4.png',screen:{goods_category_id:[13],goods_category_extend_id:[],brand_id:null}},
      {id: 7,icon: 'iconfont iconicon--',title: '白兰地',link: '',img: '../../assets/img/m9.png',screen:{goods_category_id:[12],goods_category_extend_id:[],brand_id:null}},
      {id: 8,icon: 'iconfont iconicon--',title: '日本清酒',link: '',img: '../../assets/img/m8.png',screen:{goods_category_id:[7],goods_category_extend_id:[],brand_id:null}},
      // {id: 9,icon: 'iconfont iconicon--',title: '每日清仓',link: '',img: '../../assets/img/m11.png',screen:{goods_category_id:null,goods_category_extend_id:[],brand_id:null}}
    ],
    // 推荐
    recommend:[
      {id:1,src: '../../assets/img/recomment1.jpg',title:'张三'},
      {id:2,src: '../../assets/img/recomment2.jpg',title:'张三'},
      {id:3,src: '../../assets/img/recomment1.jpg',title:'张三'},
      {id:4,src: '../../assets/img/recomment2.jpg',title:'张三'}
    ],
    // 头条
    headlines:[
      {id:1,text: '【优惠】领券专区抢优惠，最高立减100元'},
      {id:2,text: '【优惠】领券专区抢优惠，最高立减200元，机会不容错过'}
    ],
    // 热门品牌
    bigHot: {
      title: '次新茅台',
      describe: '酱香经典 玉液琼浆',
      img: '../../assets/img/hot1.png'
    },
    smallHot: [
      {id:1,title: '彩色茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
      {id:2,title: '生肖茅台',describe: '陈年酿酒 艺术珍品',img: '../../assets/img/hot1.png'}
    ],
    // 品牌专区
    recommendBrand:[
      {id:1,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
      {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
      {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'}
    ],
    brandLogo: [],
    // 专区
    zones:[
      {
        id:1,
        title: '茅台专区',
        color: '#FF9019',
        content: [
          {id:1,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'},
          {id:1,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'}
        ],
      },
      {
        id:2,
        title: '五粮液专区',
        color: '#8B75F5',
        content: [
          {id:1,title: '五粮液',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'},
          {id:1,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'}
        ],
      },
      {
        id:3,
        title: '送礼专区',
        color: '#0BCBA4',
        content: [
          {id:1,title: '送礼',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'},
          {id:1,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'}
        ],
      },
      {
        id:4,
        title: '地方名酒',
        color: '#5BA5FE',
        content: [
          {id:1,title: '地方',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:2,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:3,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'},
          {id:4,title: '茅台',describe: '大牌名酒 新品汇聚',img: '../../assets/img/hot1.png'},
          {id:5,title: '五粮液',describe: '五粮精华 浓香佳酿',img: '../../assets/img/hot1.png'},
          {id:6,title: '剑南春',describe: '盛唐名酒 代代相传',img: '../../assets/img/hot1.png'}
        ],
      }
    ],
    // 猜你喜欢
    guess:[
      {id:1,title: '推荐',describe: '猜你喜欢',screen: {is_recommend: 1,allrand: 1}},
      {id:4,title: '陈年老酒',describe: '历久弥香',screen: {is_recommend: 1,goods_category_id: [2],goods_category_extend_id: [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]}},
      {id:3,title: '白酒',describe: '最受欢迎',screen: {is_recommend: 1,goods_category_id: [2],goods_category_extend_id: [2,6,12,13,14]}},
      {id:5,title: '红酒',describe: '葡萄世界',screen: {is_recommend: 1,goods_category_id: [5]}},
      {id:6,title: '洋酒',describe: '世界名酒',screen: {is_recommend: 1,goods_category_id: [8,12,13,14,15,16,17,18,19]}},
      {id:7,title: '啤酒',describe: '进口精酿',screen: {is_recommend: 1,goods_category_id: [27]}},
      {id:8,title: '日本清酒',describe: '精米步合',screen: {is_recommend: 1,goods_category_id: [7]}},
      {id:9,title: '威士忌',describe: '飘逸猛烈',screen: {is_recommend: 1,goods_category_id: [13]}},
      {id:10,title: '白兰地',describe: '醇香佳酿',screen: {is_recommend: 1,goods_category_id: [12]}},
      {id:11,title: '伏特加',describe: '生命之水',screen: {is_recommend: 1,goods_category_id: [14]}},
      {id:12,title: '朗姆酒',describe: '纳尔逊之血',screen: {is_recommend: 1,goods_category_id: [15]}},
      {id:13,title: '利口酒',describe: '液体宝石',screen: {is_recommend: 1,goods_category_id: [18]}},
      {id:14,title: '龙舌兰',describe: '墨西哥国酒',screen: {is_recommend: 1,goods_category_id: [16]}},
      {id:15,title: '杜松子酒',describe: '鸡尾酒心脏',screen: {is_recommend: 1,goods_category_id: [17]}},
      {id:16,title: '烧酒',describe: '日韩烧酒',screen: {is_recommend: 1,goods_category_id: [8]}},
      {id:17,title: '保健酒',describe: '陈年药酒',screen: {is_recommend: 1,goods_category_id: [11]}},
      {id:18,title: '黄酒',describe: '古老国粹',screen: {is_recommend: 1,goods_category_id: [9]}},
      {id:19,title: '香槟',describe: '葡萄酒之王',screen: {is_recommend: 1,goods_category_id: [20]}},
      {id:20,title: '酒具',describe: '各类利器',screen: {is_recommend: 1,goods_category_id: [26]}},
      {id:2,title: '上新',describe: '捡漏要快',screen: {is_recommend:1,goods_category_id:[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]}},
    ],
    current: 1,
    screen: {is_recommend: 1,allrand: 1},
    // 猜你喜欢数据
    guessDataList: [],
    // 是否有数据
    haveData: true,
    // 猜你喜欢分页
    pagination:{
      pageNum: 1,  //分页初始值，页号1
      pageSize: 10, //分页初始值，每页20条
      total: null
    },
    isTiptrue: true,
    // 添加到我的小程序显示
    guideIsShow: false,
    // 新闻数据
    newsListData: [],
    // 新闻分页
    newsPagination:{
      pageNum: 1,
      pageSize: 10, 
      total: null
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户分享
   */
  onShareAppMessage: function () {
    return {
      title: '常樂酒行',
      path: '/pages/index/index'
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.haveData){
      let newPageNum = this.data.pagination.pageNum + 1;
      this.setData({
        'pagination.pageNum': newPageNum
      })
      this.getGuessListData(this.data.current,this.data.screen,this.data.pagination);
    }
    
  },
  onLoad: function () {
    let that = this;
    // 添加到我的小程序提示引导
    let firstOpen = wx.getStorageSync("loadOpen");
    if (firstOpen == undefined || firstOpen == '') { // 根据缓存周期决定是否显示新手引导      
      this.setData({ isTiptrue: true })    
    } 
    else {      
      this.setData({ isTiptrue: false })    
    }
    // 获取搜索板块高度
    wx.createSelectorQuery().select('#search').boundingClientRect(rect=>{  
      that.setData({
        searchHeight: rect.height
      })
    }).exec(); 
    // 获取猜你喜欢数据
    this.getGuessListData(this.data.current,this.data.screen,this.data.pagination);
    // 获取常樂头条数据
    this.getNewsListData(this.data.newsPagination);
    // 获取品牌列表(59:白酒)
    this.getBrandListData([2]);
  },
  // 背景不滚动
  dontMove: function(){
    return;
  },
  // Page中添加关闭引导
  closeGuideForever: function (e) {    
    wx.setStorage({      
      key: 'loadOpen', 
      data: 'OpenTwo'    
    })    
    this.setData({ isTiptrue: false }) 
  },
  // 添加到我的小程序引导按钮
  showGuide:function(){
    this.setData({
      guideIsShow: true
    })
  },
  // 添加到我的小程序引导关闭
  closeGuide:function(){
    this.setData({
      guideIsShow: false
    })
  },
  jumpToSearch: function() {
    wx.navigateTo({
       url: '/pages/search/search'
     })
   },
  //  幻灯片跳转到列表
  flashJumpToList:function(e){
    wx.reLaunch({
      url: `/pages/list/list`
    })
  },
  //  菜单跳转到列表
  jumpToList:function(e){
    wx.reLaunch({
      url: `/pages/list/list?typename=${e.currentTarget.dataset.typename}&&screen=${JSON.stringify(e.currentTarget.dataset.screen)}`
    })
  },
  // 商品圈跳转到列表页
  friendjumpToList:function(e){
    let friendId = e.currentTarget.dataset.friendid;
    let friendTitle = e.currentTarget.dataset.title;
    let friend = {
      friend_id: friendId,
      friend_title: friendTitle
    };
    wx.reLaunch({
      url: `/pages/list/list?friend=${JSON.stringify(friend)}`,
    })
  },
  // 带着关键词到搜索界面
  jumpToSearchWithKeyword:function(e){
    wx.reLaunch({
      url: '/pages/list/list?keyword='+ e.currentTarget.dataset.keyword
    })
  },
  // 菜你喜欢点击切换
  guessChange:function(e){
    let newCurrent = e.currentTarget.dataset.id;
    let current = this.data.current;
    let screen = {};
    if(current != newCurrent){ // 点的不是自己
      this.data.guess.forEach(item => {
        if(item.id == newCurrent){
          screen = item.screen;
        }
      })
      this.setData({
        screen,
        'pagination.pageNum': 1,
        'pagination.pageSize': 10,
        'pagination.total': null,
      })
      this.getGuessListData(newCurrent,screen,this.data.pagination);
    }
  },
  getGuessListData:function(newCurrent,screen,pagination){
    let guessDataListArr = []
    let current = this.data.current;
    if(newCurrent == current){
      guessDataListArr = this.data.guessDataList
    }
    // 数据已经到底
    if( guessDataListArr.length == this.data.pagination.total){
      wx.showToast({
        title: '没有更多了~',
        icon: 'none',
        duration: 1500
      })
    }else{
      api.getGuessListRandData(screen,pagination).then(res => {
        if(res.code == 0){
          if(res.data.total != 0){
            guessDataListArr.push(...res.data.list);
            this.setData({
              current: newCurrent,
              guessDataList: guessDataListArr,
              'pagination.total': res.data.total
            })
          }else{
            if(newCurrent != current){
              wx.showToast({
                title: '没有找到相关商品，为你推荐了其他商品',
                icon: 'none',
                duration: 1500
              })
            }
            this.getGuessListData(newCurrent,{},pagination);
          }
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          })
        }
      })
    }
  },
  // 获取常樂头条数据
  getNewsListData:function(pagination){
    api.getNewsListData(pagination).then(res => {
      if(res.code == 0){
        this.setData({
          newsListData: res.data.data.list
        })
      }
    })
  },
  // 跳转到新闻详情
  jumpToNews:function(e){
    let articleId = e.currentTarget.dataset.id;
    if(articleId){
      wx.navigateTo({
        url: `/pages/news/news?id=${articleId}`
      })
    }else{
      wx.navigateTo({
        url: `/pages/news/news`
      })
    }
    
  },
  // 获取品牌列表
  getBrandListData:function(goods_category_id){
    let newBrandListData = [];
    api.getBrandList(goods_category_id).then(res => {
      if(res.code == 0){
        for(let i = 0; i < 10; i++){
          newBrandListData[i] = res.data[0].childrens[i];
        }
        this.setData({
          brandLogo: newBrandListData
        })
      }
    })
  },
  // 点击商品跳转到商品详情页
  jumpToGoods:function(e){
    let goodsId = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '/pages/goods/goods?goods_id='+goodsId
    })
  }
})

